home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / lseek.c < prev    next >
C/C++ Source or Header  |  1993-10-11  |  2KB  |  85 lines

  1. #include <compiler.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <osbind.h>
  6. #include <memory.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include "lib.h"
  10.  
  11. /*
  12.  * emulate berzerkly lseek too
  13.  */
  14. long lseek(handle, offset, mode)
  15. int handle;
  16. long offset;
  17. int mode;
  18. {
  19.     long current_pos;
  20.     long expected_pos;
  21.     long new_pos;
  22.     char buf[256];
  23.     
  24.     if ( (mode == SEEK_END) || (offset <= 0) )
  25.     /* do it the usual way */
  26.       {
  27.     current_pos = Fseek (offset, handle, mode);
  28.     if (current_pos < 0)
  29.       {
  30.         errno = (int) -current_pos;
  31.         return -1L;
  32.       }
  33.     return current_pos;
  34.       }
  35.     
  36.     current_pos = Fseek (0L, handle, SEEK_CUR); /* find out where we are */
  37.     if (current_pos < 0)
  38.       {
  39.     /* a real error, e.g. an unseekable device */
  40.     errno = (int) -current_pos;
  41.     return -1L;
  42.       }
  43.  
  44.     if (mode == SEEK_SET)
  45.     expected_pos = offset;
  46.     else
  47.     expected_pos = offset + current_pos;
  48.     new_pos = Fseek (offset, handle, mode);
  49.     if (new_pos == expected_pos)
  50.     return(new_pos);
  51.     
  52.     /* otherwise extend file -- zero filling the hole */
  53.     if (new_pos < 0)        /* error? */
  54.     {
  55.     new_pos = Fseek (0L, handle, SEEK_END);    /* go to eof */
  56.     }    
  57.     
  58.     bzero(buf, (size_t)256);
  59.     while (expected_pos > new_pos)    
  60.     {
  61.     offset = expected_pos - new_pos;
  62.     if (offset > 256) 
  63.         offset = 256;
  64.     if((current_pos = _write(handle, buf, offset)) != offset)
  65.         return((current_pos > 0) ? (new_pos + current_pos) :
  66.            -1L);    /* errno set by write */
  67.     new_pos += offset;
  68.     }
  69.     return(new_pos);  
  70. }
  71.  
  72. long tell(h)
  73. int h;
  74. {
  75.     register long rv;
  76.     
  77.     rv = Fseek(0L, h, SEEK_CUR);
  78.     if(rv < 0)
  79.       {
  80.     errno = ((int) -rv);
  81.     return -1L;
  82.       }
  83.     return(rv);
  84. }
  85.